package com.dm.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.dm.server.common.UserConstant;
import com.dm.server.dto.LoginFormDTO;
import com.dm.server.dto.Result;
import com.dm.server.dto.UserDTO;

import com.dm.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    //@Resource
    //private IUserInfoService userInfoService;

    @PostMapping("code")
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session) {
        // TODO 发送短信验证码并保存验证码
        return userService.sendCode(phone, session);
    }

    @PostMapping("doLogin")
    public Result doLogin(@RequestBody LoginFormDTO loginForm) {
        return userService.login(loginForm);
    }

/*
    @PostMapping("/vip/{id}")
    public Result visitVip(@PathVariable("id") Long id) {
        boolean isOk = StpUtil.hasPermission(UserConstant.USER_VIP + id);
        if (isOk) {
            return Result.ok();
        }
        return Result.fail(UserConstant.NO_PERMISSION);
    }

    @SaCheckLogin
    @PostMapping("/afterLogin/{id}")
    public Result afterLogin() {

        return Result.ok();
    }


    @PostMapping("/logout")
    public Result logout() {
        UserDTO user = UserHolder.getUser();
        return Result.ok(user);
    }

    @GetMapping("/me")
    public Result me() {
        // TODO 获取当前登录的用户并返回
        UserDTO user = UserHolder.getUser();
        return Result.fail("功能未完成");
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId) {
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }

    @PostMapping("/sign")
    public Result sign() {
        return userService.sign();
    }

    @GetMapping("/sign/count")
    public Result signCount() {
        return userService.signCount();
    }*/

}
